/**
 * Captain
 * © 2017 Captain copyright，All rights reserved.
 * http://www.sccaptain.com.cn
 * 
 * JAVA : 8
 * 文  件  名: DataDictionaryServiceImpl.java
 * 创  建  人: sunfeng (sun.feng@sccaptain.com.cn)
 * 创建时间: 2017年7月4日 下午3:39:38
 * 版         本: 1.0.0
 * 备         注:
 * 修订历史:
 */
package com.material.c2c.sysconfig.service;

import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cache.Cache;
import org.springframework.cache.Cache.ValueWrapper;
import org.springframework.cache.CacheManager;
import org.springframework.cache.annotation.CacheEvict;
import org.springframework.cache.annotation.Cacheable;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.Assert;

import com.material.c2c.sysconfig.dao.DataDictionaryDao;
import com.material.c2c.sysconfig.entity.XTPZ_SJZD;
import com.material.c2c.sysconfig.model.DataDictionaryDTO;
import com.material.c2c.util.BeanUtils;

/**
 * 数据字典业务接口实现类。
 * 
 * @since 1.0
 * @version 1.0
 * @author sunfeng (sun.feng@sccaptain.com.cn)
 */
@Service
@Transactional
public class DataDictionaryServiceImpl implements DataDictionaryService {

  @Autowired
  private DataDictionaryDao dataDictionaryDao;

  @Autowired
  private CacheManager cacheManager;

  @Override
  @Cacheable(cacheNames = "dataDictionaryCache", key = "'dictionaryItemList'")
  public List<XTPZ_SJZD> findAllDataDictionaryItem() {
    return dataDictionaryDao.findAllDataDictionaryItem();
  }

  @Override
  @Cacheable(cacheNames = "dataDictionaryCache", key = "#itemId + ':dictionaryItemValueList'")
  public List<DataDictionaryDTO> findDictionaryItemValueByItemId(String itemId) {
    return dataDictionaryDao.findDictionaryItemValueByItemId(itemId);
  }

  @Override
  public boolean validateBmExists(String itemId, String bm, String valueId) {
    List<XTPZ_SJZD> entityList = dataDictionaryDao.findBySjidAndBmAndIdNot(itemId, bm, valueId);
    // 返回的结果集不为null并且不为空，表示查询到记录，那就返回true。
    if (null != entityList && !entityList.isEmpty()) {
      return true;
    }
    return false;
  }

  @Override
  @CacheEvict(cacheNames = "dataDictionaryCache", key = "#entity.sjid + ':dictionaryItemValueList'")
  public void add(XTPZ_SJZD entity) {
    dataDictionaryDao.add(entity);
  }

  @Override
  public int getMaxBmLengthByDictionaryItemValueList(List<DataDictionaryDTO> itemValueList) {
    int maxBmLength = 0;
    if (null != itemValueList && !itemValueList.isEmpty()) {
      for (DataDictionaryDTO dictionaryDTO : itemValueList) {
        if (dictionaryDTO.getBm().length() > maxBmLength) {
          maxBmLength = dictionaryDTO.getBm().length();
        }
      }
    }
    return maxBmLength;
  }

  @Override
  @CacheEvict(cacheNames = "dataDictionaryCache", key = "#sourceEntity.sjid + ':dictionaryItemValueList'")
  public void update(XTPZ_SJZD sourceEntity, XTPZ_SJZD inputs) {
    // 如果是内置的字典项值，是否自定义标识为0，此时修改的名称只能作为自定义名称
    if (!sourceEntity.getSfzdy().booleanValue()) {
      sourceEntity.setZdymc(inputs.getMc());
    } else if (sourceEntity.getSfzdy().booleanValue()) {// 如果是自定义的字典项值，是否自定义标识为1，此时修改的名称需要修改自定义名称和名称两个属性
      sourceEntity.setMc(inputs.getMc());
      sourceEntity.setZdymc(inputs.getMc());
    }
    // 修改备注
    sourceEntity.setBz(inputs.getBz());
    dataDictionaryDao.update(sourceEntity);
  }

  @Override
  public XTPZ_SJZD getEntityById(String id) {
    return dataDictionaryDao.getOne(id);
  }

  @SuppressWarnings("unchecked")
  @Override
  public XTPZ_SJZD findDictionaryItemValueEntity(String itemBm, String valueBm) {
    Assert.notNull(itemBm, "字典项编码不能为空");
    Assert.notNull(valueBm, "字典值编码不能为空");
    // 获取数据字典的缓存实例对象
    Cache dataDictionaryCache = cacheManager.getCache("dataDictionaryCache");
    // 数据字典项列表
    List<XTPZ_SJZD> dictionaryItems = null;
    // 首先获取缓存中是否存储有字典项列表
    ValueWrapper dictionaryItemListValueWrapper = dataDictionaryCache.get("dictionaryItemList");
    if (null == dictionaryItemListValueWrapper) {
      dictionaryItems = dataDictionaryDao.findAllDataDictionaryItem();
      if (!dictionaryItems.isEmpty()) {
        dataDictionaryCache.put("dictionaryItemList", dictionaryItems);
      }
    } else {
      dictionaryItems = (List<XTPZ_SJZD>) dictionaryItemListValueWrapper.get();
    }
    for (XTPZ_SJZD dictionaryItem : dictionaryItems) {
      if (dictionaryItem.getBm().equals(itemBm)) {
        // 指定字典项下的值列表
        List<DataDictionaryDTO> dictionaryItemValues = null;
        // 从缓存中获取该字典项的值列表
        ValueWrapper dictionaryItemValueListWrapper = dataDictionaryCache
            .get(dictionaryItem.getId() + ":dictionaryItemValueList");
        if (null == dictionaryItemValueListWrapper) {
          dictionaryItemValues = dataDictionaryDao.findDictionaryItemValueByItemId(dictionaryItem.getId());
          if (!dictionaryItemValues.isEmpty()) {
            dataDictionaryCache.put(dictionaryItem.getId() + ":dictionaryItemValueList", dictionaryItemValues);
          }
        } else {
          dictionaryItemValues = (List<DataDictionaryDTO>) dictionaryItemValueListWrapper.get();
        }
        for (DataDictionaryDTO dataDictionaryDTO : dictionaryItemValues) {
          if (dataDictionaryDTO.getBm().equals(valueBm)) {
            XTPZ_SJZD entity = new XTPZ_SJZD();
            BeanUtils.copyBeanNotNull2Bean(dataDictionaryDTO, entity);
            return entity;
          }
        }
      }
    }
    return null;
  }
}
